/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aternity.vbd.ui;

import com.aternity.vbd.mgmt.events.*;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import dnl.utils.swing.Colors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Daniel Orr
 */
@Component
public class GeneralPanel extends javax.swing.JPanel {

    @Autowired
    EventBus eventBus;

    /**
     * Creates new form GeneralPanel
     */
    public GeneralPanel() {
        initComponents();
    }

    @Subscribe
    public void onConnectionSuccess(ConnectionSucceededEvent connectionSucceededEvent) {
        connectedToLabel.setForeground(Colors.imperialBlue);
        connectedToLabel.setText(connectionSucceededEvent.getConnectionUrl());
    }

    @Subscribe
    public void onConnectionFailure(ConnectionFailedEvent connectionFailedEvent) {
        connectedToLabel.setForeground(Colors.angryRed);
        connectedToLabel.setText("Connection Failure");
    }

    @Subscribe
    public void onDbStatus(DbStatusEvent dbStatusEvent) {
        switch (dbStatusEvent.getDbStatus()) {
            case PARTIAL:
                createTablesJB.setEnabled(true);
                createTablesJB.setText("Recreate Tables");
                tablesStatusLabel.setForeground(Colors.angryRed);
                tablesStatusLabel.setText("Partial or old tables");
                break;
            case NO_DDL_DATA:
                createTablesJB.setEnabled(false);
                tablesStatusLabel.setForeground(Colors.angryRed);
                tablesStatusLabel.setText("DDL scripts not found");
                break;
            case TABLES_NOT_EXIST:
                createTablesJB.setEnabled(true);
                createTablesJB.setText("Create Tables");
                tablesStatusLabel.setForeground(Colors.angryRed);
                tablesStatusLabel.setText("Not installed");
                break;
            case TABLES_EXIST:
                createTablesJB.setEnabled(true);
                createTablesJB.setText("Recreate Tables");
                tablesStatusLabel.setForeground(Colors.imperialBlue);
                tablesStatusLabel.setText("OK");
                break;
        }

    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        connectedToLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tablesStatusLabel = new javax.swing.JLabel();
        createTablesJB = new javax.swing.JButton();
        deleteTablesJB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jLabel1.setText("Using DB at:");

        connectedToLabel.setForeground(new java.awt.Color(0, 0, 255));
        connectedToLabel.setText("?");

        jLabel2.setText("VBD Tables Status:");

        tablesStatusLabel.setForeground(new java.awt.Color(0, 0, 255));
        tablesStatusLabel.setText("?");

        createTablesJB.setText("Create Tables");
        createTablesJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createTablesJBActionPerformed(evt);
            }
        });

        deleteTablesJB.setText("Delete Tables");
        deleteTablesJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteTablesJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(29, 29, 29))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                                .addGap(3, 3, 3)
                                                .addComponent(createTablesJB)
                                                .addGap(18, 18, 18)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(deleteTablesJB)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(connectedToLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(tablesStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(288, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(connectedToLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(32, 32, 32)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(tablesStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(deleteTablesJB)
                                        .addComponent(createTablesJB))
                                .addContainerGap())
        );

        jSplitPane1.setTopComponent(jPanel1);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jSplitPane1.setRightComponent(jScrollPane1);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void createTablesJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createTablesJBActionPerformed
        boolean recreate = createTablesJB.getText().contains("Recreate");
        eventBus.post(new CreateDbRequest(recreate));
    }//GEN-LAST:event_createTablesJBActionPerformed

    private void deleteTablesJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteTablesJBActionPerformed
        eventBus.post(new DeleteDbRequest());
    }//GEN-LAST:event_deleteTablesJBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel connectedToLabel;
    private javax.swing.JButton createTablesJB;
    private javax.swing.JButton deleteTablesJB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel tablesStatusLabel;
    // End of variables declaration//GEN-END:variables
}
